<?php
namespace Seller\Controller;

class ReportController extends BaseController{
	public $begin;
	public $end;
	public $store_id;
	public function _initialize(){
        parent::_initialize();
		$timegap = I('timegap');
		$timegap = str_replace('+',' ',$timegap);
	
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin = $gap[0];
			$end = $gap[1];
		}else{
			$lastweek = date('Y-m-d 00:00:00',strtotime("-15 day"));//30天前
			$begin = I('begin',$lastweek);
			$end =  I('end',date('Y-m-d 23:59:59'));
		}
		$this->begin = strtotime($begin);
		$this->end = strtotime($end);//+86399;
	
		$this->store_id = STORE_ID;
		$this->assign('timegap',date('Y-m-d H:i',$this->begin).' - '.date('Y-m-d H:i',$this->end));
		
	}
	
	public function index(){
		$now = strtotime(date('Y-m-d'));
		$today['today_amount'] = M('order')->where("add_time>$now and store_id=$this->store_id and order_status in(0,1,2,4)")->sum('total_amount');//今日销售总额
		$today['today_order'] = M('order')->where("add_time>$now and store_id=$this->store_id")->count();//今日订单数
		$today['cancel_order'] = M('order')->where("add_time>$now AND order_status=3 and store_id=$this->store_id")->count();//今日取消订单
		$today['sign'] = round($today['total_amount']/$today['today_order'],2);
		$this->assign('today',$today);
	
		$sql = "SELECT COUNT(*) as tnum,sum(total_amount) as amount, FROM_UNIXTIME(add_time,'%Y-%m-%d') as gap from  __PREFIX__order ";
		$sql .= " where add_time>$this->begin and add_time<$this->end and store_id=$this->store_id and order_status in(0,1,2,4) group by gap";
		$res = M()->query($sql);//订单数,交易额
		
		foreach ($res as $val){
			$arr[$val['gap']] = $val['tnum'];
			$brr[$val['gap']] = $val['amount'];
			$tnum += $val['tnum'];
			$tamount += $val['amount'];
		}

			for($i=$this->end;$i>=$this->begin;$i=$i-24*3600){
			$tmp_num = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
			$tmp_amount = empty($brr[date('Y-m-d',$i)]) ? 0 : $brr[date('Y-m-d',$i)];
			$tmp_sign = empty($tmp_num) ? 0 : round($tmp_amount/$tmp_num,2);						
			$order_arr[] = $tmp_num;
			$amount_arr[] = $tmp_amount;			
			$sign_arr[] = $tmp_sign;
			$date = date('Y-m-d',$i);
			$list[] = array('day'=>$date,'order_num'=>$tmp_num,'amount'=>$tmp_amount,'sign'=>$tmp_sign,'end'=>date('Y-m-d',$i+24*60*60));
			$day[] = $date;
		}
		$count['heji_price'] =$today['today_amount'] = M('order')->where("add_time>$this->begin and add_time<$this->end and store_id=$this->store_id and order_status in(0,1,2,4)")->sum('total_amount');//今日销售总额;
		$this->assign('list',$list);
		$result = array('order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day);
		$this->assign('result',json_encode($result));
		$this->assign('count',$count);
		$this->display();
	}

	public function saleTop(){
	    $data = I('');
	    $where = "b.order_status in(0,1,2,4) and a.store_id=$this->store_id and b.add_time>=$this->begin and b.add_time<=$this->end";
	    if(!empty($data['goods_name'])){
	        $where .= ' and a.goods_name like "%'.$data['goods_name'].'%"';
	    }
	    if(!empty($data['store_class_cat1'])){
	        $where .= ' and g.store_cat_id1 = '.$data['store_class_cat1'];
	    }
	    if(!empty($data['store_class_cat2'])){
	        $where .= ' and g.store_cat_id2 = '.$data['store_class_cat2'];
	    }
	    
	   // die;
		$sql = "select a.goods_id,a.goods_name,sum(a.goods_num) as sale_num,sum(a.goods_num*a.goods_price) as sale_amount,b.add_time from __PREFIX__order_goods a LEFT JOIN __PREFIX__order b ON a.order_id=b.order_id LEFT JOIN __PREFIX__goods g ON g.goods_id = a.goods_id";
		$sql .=" where $where group by a.goods_id order by sale_amount DESC limit 100";
		$res = M()->cache(true,3600)->query($sql);
		foreach($res as $key=>$v){
			$res[$key]['dianji']=M('goods')->where('goods_id='.$v['goods_id'])->getField('click_count');
		}
		
		
		//获取一级分类
		$store_class_cat1 = M('store_goods_class')->where(['store_id'=>STORE_ID,'parent_id'=>0])->select();
		$store_class_cat2 = [];
		if($data['store_class_cat1']){
		    $store_class_cat2 = M('store_goods_class')->where(['store_id'=>STORE_ID,'parent_id'=>$data['store_class_cat1']])->select();
		}
		

		$this->assign('store_class_cat1',$store_class_cat1);
		$this->assign('store_class_cat2',$store_class_cat2);
		$this->assign('list',$res);
		$this->display(); 
	}
	
	public function export_saleTop(){
	     $data = I('');
	   
	    $where = "b.order_status in(0,1,2,4) and a.store_id=$this->store_id and b.add_time>=$this->begin and b.add_time<=$this->end";
	    if(!empty($data['goods_name']) && $data['goods_name'] !='undefined'){
	        $where .= ' and a.goods_name like "%'.$data['goods_name'].'%"';
	    }
	    if(!empty($data['store_class_cat1'])){
	        $where .= ' and g.store_cat_id1 = '.$data['store_class_cat1'];
	    }
	    if(!empty($data['store_class_cat2'])){
	        $where .= ' and g.store_cat_id2 = '.$data['store_class_cat2'];
	    }
	    
	   // die;
		$sql = "select a.goods_id,a.goods_name,sum(a.goods_num) as sale_num,sum(a.goods_num*a.goods_price) as sale_amount,b.add_time from __PREFIX__order_goods a LEFT JOIN __PREFIX__order b ON a.order_id=b.order_id LEFT JOIN __PREFIX__goods g ON g.goods_id = a.goods_id";
		$sql .=" where $where group by a.goods_id order by sale_amount DESC limit 100";
		$res = M()->cache(true,3600)->query($sql);
		foreach($res as $key=>$v){
			$res[$key]['dianji']=M('goods')->where('goods_id='.$v['goods_id'])->getField('click_count');
		}
	
	    $strTable ='<table width="500" border="1">';
    	$strTable .= '<tr>';
    	$strTable .= '<td style="text-align:center;font-size:12px;width:120px;">排行</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;width:120px;">商品名称</td>';
		$strTable .= '<td style="text-align:center;font-size:12px;width:120px;">点击率</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;" width="*">销售量</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;" width="*">销售额</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;" width="*">均价</td>';
    	$strTable .= '</tr>';
    	

    	foreach($res as $key=>$val){
    	    $num = $key+1;
    		$strTable .= '<tr>';
    		$strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$num.'</td>';
    		$strTable .= '<td style="text-align:center;font-size:12px;">'.$val['goods_name'].'</td>';
			$strTable .= '<td style="text-align:center;font-size:12px;">'.$val['dianji'].'</td>';
            $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['sale_num'].'</td>';
            $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['sale_amount'].' </td>';
            $strTable .= '<td style="text-align:left;font-size:12px;">'.geshi($vo['sale_amount']/$vo['sale_num']).'</td>';
    		$strTable .= '</tr>';
    	}
	 	$strTable .='</table>';
    	unset($orderList);
    	downloadExcel($strTable,'商品分析');
    	exit();
	}
    public function get_category(){
        $parent_id = I('get.parent_id',0); // 商品分类 父id  
        empty($parent_id) && exit('');
        $list = M('store_goods_class')->where(array('parent_id'=>$parent_id))->select();
        foreach($list as $k => $v)
        {
            // 如果是某个店铺登录的, 那么这个店铺只能看到自己申请的分类,其余的看不到
/*             if($class_id && !in_array($v['id'],$class_id))
                continue; */
            $html .= "<option value='{$v['cat_id']}' >{$v['cat_name']}</option>";
        }
        exit($html);
    } 
	
	public function saleTop_info(){
		$p = I('p',1); 
		$start = ($p-1)*30;
		
		$res = M()->query("select a.order_id,a.goods_id,a.goods_name,a.goods_num,a.member_goods_price,a.spec_key_name,a.spec_key,b.add_time from __PREFIX__order_goods a LEFT JOIN __PREFIX__order b ON a.order_id=b.order_id where b.order_status in(1,2) and a.store_id=$this->store_id and b.add_time>=$this->begin and b.add_time<=$this->end group by a.spec_key order by a.rec_id DESC limit $start,30");//echo \json_encode($res);
		
		$total = M()->query("select a.goods_id,a.goods_name,a.goods_num,member_goods_price,a.spec_key_name,a.spec_key,b.add_time from __PREFIX__order_goods a LEFT JOIN __PREFIX__order b ON a.order_id=b.order_id where b.order_status in(1,2) and a.store_id=$this->store_id and b.add_time>=$this->begin and b.add_time<=$this->end group by a.spec_key");
		$price_heji=0;
		foreach($res as $key=>$v){
			$goods=M('order_goods a')
			->field('a.goods_num,a.goods_price,sum(a.goods_num) as sale_num,sum(a.goods_num*a.goods_price) as sale_amount,b.add_time')
			->join('ty_order b on a.order_id=b.order_id')
			->where("a.store_id=".$this->store_id." and b.add_time>=".$this->begin." and b.add_time<=".$this->end." and a.spec_key='".$v['spec_key']."'")->select();
			
			$res[$key]['goods_num']=$goods[0]['sale_num'];
			$res[$key]['sale_amount']=$goods[0]['sale_amount'];
		}
		
		$count = \count($total);
		$Page = new \Think\Page($count,30);
		$show = $Page->show();
		$this->assign('page',$show);
		$this->assign('list',$res);
		$this->display();
	}
	
	public function jiagou(){
		$res=M('cart')->cache(true,3600)
		->field('goods_id,goods_name,spec_key_name,member_goods_price,goods_num,add_time,(member_goods_price*goods_num) as jiage')
		->where("store_id=".$this->store_id." and add_time>=$this->begin and add_time<=$this->end")
		->group('goods_id')
		->limit(150)
		->order('jiage desc')
		->select();
		
		$this->assign('list',$res);
		$this->display();
	}

	public function saleList(){
	   
		$p = I('p',0);
		$start = $p*20;
		$sql = "select a.*,b.order_sn,b.shipping_name,b.pay_name,b.add_time from __PREFIX__order_goods as a left join __PREFIX__order as b on a.order_id=b.order_id ";
		$sql .= " where b.add_time>$this->begin and b.add_time<$this->end and a.store_id=$this->store_id and b.order_status in(0,1,2,4) order by add_time limit $start,20";
		$res = M()->cache(true)->query($sql);
		foreach($res as $key=>$v){
			$res[$key]['dianji']=M('goods')->where('goods_id='.$v['goods_id'])->getField('click_count');
		}
		$this->assign('list',$res);
		
		$sql2 = "select a.goods_num,a.member_goods_price from __PREFIX__order_goods as a left join __PREFIX__order as b on a.order_id=b.order_id ";
		$sql2 .= " where b.add_time>$this->begin and b.add_time<$this->end and a.store_id=$this->store_id and b.order_status in(0,1,2,4)";
		$total = M()->query($sql2);

		$heji=M()->query("select sum(total_amount) as heji from ty_order where store_id=$this->store_id and add_time>$this->begin and add_time<$this->end and order_status in(0,1,2,4)");

		$this->assign('heji',$heji[0]['heji']);
		$count = \count($total);
		$Page = new \Think\Page($count,20);
		$show = $Page->show();
		$this->assign('page',$show);
		$this->display();
	}
	
	//财务统计
	public function finance(){
		$sql = "SELECT sum(b.goods_num*b.member_goods_price) as goods_amount,sum(a.shipping_price) as shipping_amount,sum(b.goods_num*b.cost_price) as cost_price,";
		$sql .= "sum(a.coupon_price) as coupon_amount,FROM_UNIXTIME(a.add_time,'%Y-%m-%d') as gap from  __PREFIX__order a left join __PREFIX__order_goods b on a.order_id=b.order_id ";
		$sql .= " where a.add_time>$this->begin and a.add_time<$this->end AND a.store_id=$this->store_id and a.pay_status=1 and a.shipping_status=1 and b.is_send=1 group by gap order by a.add_time";
		$res = M()->cache(true)->query($sql);//物流费,交易额,成本价
		
		foreach ($res as $val){
			$arr[$val['gap']] = $val['goods_amount'];
			$brr[$val['gap']] = $val['cost_price'];
			$crr[$val['gap']] = $val['shipping_amount'];
			$drr[$val['gap']] = $val['coupon_amount'];
		}
			
		for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
			$date = $day[] = date('Y-m-d',$i);
			$tmp_goods_amount = empty($arr[$date]) ? 0 : $arr[$date];
			$tmp_cost_amount = empty($brr[$date]) ? 0 : $brr[$date];
			$tmp_shipping_amount = empty($crr[$date]) ? 0 : $crr[$date];
			$tmp_coupon_amount = empty($drr[$date]) ? 0 : $drr[$date];
			
			$goods_arr[] = $tmp_goods_amount;
			$cost_arr[] = $tmp_cost_amount;
			$shipping_arr[] = $tmp_shipping_amount;
			$coupon_arr[] = $tmp_coupon_amount;
			$list[] = array('day'=>$date,'goods_amount'=>$tmp_goods_amount,'cost_amount'=>$tmp_cost_amount,
					'shipping_amount'=>$tmp_shipping_amount,'coupon_amount'=>$tmp_coupon_amount,'end'=>date('Y-m-d',$i+24*60*60));
		}
		$this->assign('list',$list);
		$result = array('goods_arr'=>$goods_arr,'cost_arr'=>$cost_arr,'shipping_arr'=>$shipping_arr,'coupon_arr'=>$coupon_arr,'time'=>$day);
		$this->assign('result',json_encode($result));
		$this->display();
	}
	
	public function visit(){
		$this->display();
	}
	
}